{% extends "base.html" %}
{% load form_utils_tags %}

{% block title %}Vnos vpisnega lista{% endblock %}

{% block js %}
<script src="{{ MEDIA_URL }}js/search.js"></script>
{% endblock %}

{% block content %}

{% if added %}
<div class="notification successful">
  <div class="title">Uspešno vnešeno</div>
  <div class="content">
    Vnos študenta je bil uspešno končan.
  </div>
</div>
{% endif %}

{% if enrollment_entry %}
{% if not request.user.student %}<button class="search_students">Poišči študenta</button>{% endif %}
<form id="enrollment_sheet_form" method='post' enctype="multipart/form-data">
  <input type="hidden" name="student" value="{% if user.student %}{{ user.student.pk }}{% else %}{{enrollment_entry.student.pk}}{% endif %}" />
  {{ enrollment_entry|render }}
  {% csrf_token %}
  <input type="submit" value="Vnesi" />
</form>
{% else %}
Vi ste že vpisani v tekoče šolsko leto.
{% endif %}

{% if not request.user.student %}
<script>
$(document).ready(function() {
  $('button.search_students').click(function() {search_show('student');});
  
  search_setcb('student', function(student) {
    var enrollment_sheet = student.fields.latest_enrollment_sheet;
    
    $('#enrollment_sheet_form p').each(function(i, field) {
      var input = $(field).find('input');
      if (input.length > 0) {
        /* We have a input */
        /* Get key and check if we have that information */
        var key = input.attr('name');
        if (enrollment_sheet.fields[key] == undefined)
          return;
        
        input.val(enrollment_sheet.fields[key]);
      } else {
        /* We have a select */
        input = $(field).find('select');
        var key = input.attr('name');
        if (enrollment_sheet.fields[key] == undefined)
          return;
        
        input.val(enrollment_sheet.fields[key]);
      }
    });
    
    /* Set the student's pk */
    if (student.fields.latest_enrollment_sheet.fields.emso.match(/\d{8}0{5}/)) {
      $('#enrollment_sheet_form input[name="student"]').val(student.pk);
      $('#enrollment_sheet_form input[name="student"]').removeAttr('disabled');
    } else {
      $('#enrollment_sheet_form input[name="student"]').attr('disabled', 'true');
    }
    
    search_hide('student');
  }, true);
});
</script>
{% endif %}

{% endblock %}>